<?php

/**
 * Created by PhpStorm.
 * User: huyang
 * Date: 2017/6/6
 * Time: 上午9:00
 */
class User_model extends BS_Model
{
    private $tablename= "users";

    public function __construct()
    {
        parent::__construct();
    }

    // 注册新增
    public function add($data)
    {
        $this->db->insert($this->tablename, $data);
        return $this->db->insert_id();
    }

    // 绑定银行卡
    public function bindBank($uid, $cardid, $bankid, $username,$bankinfo)
    {
        $this->db->set('cardid', $cardid);
        $this->db->set('bankid', empty($bankid)?0:$bankid);
        $this->db->set('username', $username);
        $this->db->set('bankinfo',$bankinfo);
        $this->db->where('uid', $uid);
        $this->db->update('users');
        return $this->db->affected_rows();
    }


    // 修改密码
    public function updatePassword($mobile, $password)
    {
        $this->db->set('password', sha1($password));
        $this->db->where('mobile', $mobile);
        $this->db->update('users');
    }

    // 验证账号是否存在
    function checkMobile($mobile) {
        $this->db->select('*');
        $this->db->from($this->tablename);
        $this->db->where('mobile', $mobile);
        $query = $this->db->get();
        if ($query->num_rows() == 0) {
            return null;
        }
        return $query->row();
    }


    // 验证密码
    function confirmPassword($mobile, $password)
    {
        $this->db->select('*');
        $this->db->from('users');
        $this->db->where('mobile', $mobile);
        $this->db->where('password', sha1($password));
        $query = $this->db->get();
        if ($query->num_rows() == 0)
        {
            return NULL;
        }
        return $query->row_array();
    }

    // 删除
    public function deleteUser($uid)
    {
        $this->db->where('uid', $uid);
        $this->db->delete('users');
    }

    //根据UID查询用户信息
    public function getUserByUid($uid = null)
    {
        $this->db->select('*');
        $this->db->from('users');
        $this->db->where('uid', $uid);
        $query = $this->db->get();
        if ($query->num_rows() == 0)
        {
            return array();
        }
        //echo $this->db->last_query();
        return $query->row_array();
    }

    //根据手机号码查询用户信息
    public function getUserByMobile($mobile = null)
    {
        $this->db->select('*');
        $this->db->from('users');
        $this->db->where('mobile', $mobile);
        $query = $this->db->get();
        if ($query->num_rows() != 0)
        {
            return $query->result_array();
        }
        return array();
    }

    // 设置默认地址id
    public function setAddressid($uid, $address_id)
    {
        $this->db->set('address_id', $address_id);
        $this->db->where('uid', $uid);
        return $this->db->update('users');
    }

    // 获取所有用户信息
    public function getAllUser()
    {
        $this->db->select('*');
        $this->db->from('users');
        $query = $this->db->get();
        if ($query->num_rows() == 0)
        {
            return array();
        }
        return $query->result_array();
    }

    /**
     * 更新user用户最后登陆时间
     * @param  int $uid 用户id
     * @return bool      TRUE on success, FALSE on failure
     */
    public function updateLasttimeByUid($uid, $ip)
    {
        //更新为当前时间戳
        $time_now = time();
        $result = $this->db->set('last_time', $time_now)
                           ->set('ip', $ip)
                           ->where('uid', $uid)
                           ->update('users');
        return $result;
    }

    /**
     * 根据uid获取该用户的默认收货地址
     * @param  int $uid uid
     * @return array      查不到则返回空数组
     */
    public function getDefaultAddressInfoByUid($uid)
    {
        $this->db->select('address.*');
        $this->db->from('users');
        $this->db->join('address', 'address.address_id = users.address_id');
        $this->db->where('users.uid', $uid);
        $query = $this->db->get();
        if($query->num_rows() == 0)
        {
            return array();
        }
        return $query->row_array();
    }

    /**
     * 
     */
    
    public function checkCard($uid)
    {
        $query = $this->db->from('users')->where('uid',$uid)->get();
        return $query->num_rows() > 0 ? $query->row_array() : array();
    }

}
